package com.itheima.edu.info.manager.controller;

import com.itheima.edu.info.manager.domain.Student;
import com.itheima.edu.info.manager.entry.Login;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.IDN;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Scanner;

/*
客服类:
专门和用户打交道的
*/
public abstract class BaseStudentController {
    Scanner sc = new Scanner(System.in);
    private static final Logger LOGGER= LoggerFactory.getLogger(BaseStudentController.class);
    //学生主菜单
    public final void start() throws Exception {
        LOGGER.info("学生管理系统启动了...........");
        while (true) {
            System.out.println("--------欢迎来到 <学生> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加学生  2.删除学生  3.修改学生  4.查看学生  5.退出");
            String choice = sc.next();
            switch (choice) {
                case "1":
                    addStudent();
                    //System.out.println("添加");
                    break;
                case "2":
                    deleteStudent();
                    //System.out.println("删除");
                    break;
                case "3":
                    updateStudent();
                    // System.out.println("修改");
                    break;
                case "4":
                   selectStudent();
                    //System.out.println("查询");
                    break;
                case "5":
                    LOGGER.info("退出学生管理系统...........");
                    Login login=new Login();
                    login.start();
                    break;
                default:
                    System.out.println("您的输入有误,请重新输入:");
                    break;
            }
        }
    }

    //添加学生
    public final void addStudent() throws IOException {
        String id = getId();
        Student student = getStudent(id);
        Socket socket = getSocket();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("[1]," + student.toString());
        bw.flush();
        socket.shutdownOutput();
        LOGGER.info("正在查询服务器,发出指令:[1]");
        //接收返回数据
        BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        String addFlag = br.readLine();
        LOGGER.info("接收到服务器响应返回是否添加成功->"+addFlag);
        if (addFlag.equals(true + "")) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
        socket.close();
    }
    //删除学生
    public final void deleteStudent() throws Exception {
        String id;
        ArrayList<Student> list = getStudent();
        if (list.size() == 0) {
            System.out.println("暂时无信息,请添加后操作");
            return;
        } else {
            id = inputStudentId();
            while (true) {
                System.out.println("是否删除:[y/n]");
                String deFlag = sc.next();
                if (deFlag.equals("n")) {
                    start();
                    break;
                } else if (!(deFlag.equals("n")) && !(deFlag.equals("y"))) {
                    System.out.println("输入有误,请重新输入");
                } else {
                    Socket socket = getSocket();
                    BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    bw.write("[2]," + id);
                    bw.flush();
                    socket.shutdownOutput();
                    LOGGER.info("正在查询服务器,发出指令:[2]");
                    //接收返回数据
                    BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    String message = br.readLine();
                    LOGGER.info("接收到服务器响应返回是否删除成功->" + message);
                    System.out.println(message);
                    socket.close();
                    break;
                }

            }
        }

    }

    //修改学生
    public final void updateStudent() throws Exception {
        String id;
        ArrayList<Student> list = getStudent();
        if (list.size() == 0) {
            System.out.println("暂时无信息,请添加后操作");
            return;
        }else{
            id = inputStudentId();
            Student student = getStudent(id);
            Socket socket = getSocket();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[3]," + student.toString());
            bw.flush();
            socket.shutdownOutput();
            LOGGER.info("正在查询服务器,发出指令:[3]");
            //接收返回数据
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String message = br.readLine();
            LOGGER.info("接收到服务器响应返回是否修改成功->"+message);
            System.out.println(message);
            socket.close();
        }
    }

    //查询学生
    public final void selectStudent() throws Exception {
        ArrayList<Student> list = getStudent();
        if (list.size() == 0) {
            System.out.println("暂时无信息,请添加后操作");
        } else {
            for (Student student : list) {
                System.out.println(student);

            }
        }
    }

    //查看文件里面是否有数据(间接)
    private ArrayList<Student> getStudent() throws Exception{
        Socket socket = getSocket();
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
        bw.write("[4]");
        bw.flush();
        socket.shutdownOutput();
        LOGGER.info("正在查询服务器,发出指令:[4]");
        //接收返回数据
        ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
        ArrayList<Student> list = (ArrayList<Student>) ois.readObject();
        LOGGER.info("接收到服务器响应返回查询到的数据->"+list);
        socket.close();
        return list;
    }

    //封装学生对象
    public abstract Student getStudent(String id);

    //获取一个存在的id
    public   String inputStudentId() throws IOException {
        String id;
        while (true) {
            System.out.println("请输入学生学号");
            id = sc.next();
            Socket socket = getSocket();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[5]," + id);
            bw.flush();
            socket.shutdownOutput();
            LOGGER.info("正在查询服务器,发出指令:[5]");
            //接收返回数据
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String message = br.readLine();
            LOGGER.info("接收到服务器响应返回学号是否存在->"+message);
            if (message.equals(true + "")) {
                break;
            } else {
                System.out.println("您输入的学号不存在,请重新输入");
            }
            socket.close();

        }
        return id;

    }
    //获取一个Scoket对象
    private Socket getSocket() throws IOException {
        return new Socket("127.0.0.1", 10000);
    }

    //获取一个不存在的id
    public final String getId() throws IOException {
        String id;
        while (true) {
            System.out.println("请输入学号:");
            id = sc.next();
            Socket socket = getSocket();
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            bw.write("[5]," + id);
              bw.flush();
            socket.shutdownOutput();
            LOGGER.info("正在查询服务器,发出指令:[5]");
            //接收返回数据
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            String message = br.readLine();
            LOGGER.info("接收到服务器响应返回数据源是否有此id->"+message);
            if (message.equals(true + "")) {
                System.out.println("您输入的学号已存在,请重新输入");
            } else {
                break;
            }
            socket.close();
        }

        return id;
    }

}
